package com.qffx.moviebase.view.classify;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.qffx.moviebase.R;
import com.qffx.moviebase.adapter.classify.ClassifyAdapter;
import com.qffx.moviebase.bean.classify.ArticleEntity;
import com.qffx.moviebase.bean.classify.AuthorEntity;
import com.qffx.moviebase.bean.classify.ClassifyBase;
import com.qffx.moviebase.bean.classify.ClassifyEntity;
import com.qffx.moviebase.bean.classify.SpecialEntity;
import com.qffx.moviebase.utils.retrofit.HttpService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class ClassifyFragment extends Fragment implements Callback<ClassifyEntity>, SwipeRefreshLayout.OnRefreshListener, ClassifyAdapter.ILoaderMore {
    private RecyclerView recycler;
    private ClassifyAdapter adapter;
    private SwipeRefreshLayout swipeRefreshLayout;
    List<ClassifyBase> list = new ArrayList<>();
    private int page = 1;
    private LinearLayout searchView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_classify, container, false);
        init(view);
        return view;
    }

    private void init(View view) {
        recycler = (RecyclerView) view.findViewById(R.id.classify_recycler);
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swrl);
        recycler.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        adapter = new ClassifyAdapter(list, getContext());
        recycler.setAdapter(adapter);
        searchView = (LinearLayout) view.findViewById(R.id.classify_search);
        searchView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), ClasifySearchActivity.class));


            }
        });

        getData();

        swipeRefreshLayout.setOnRefreshListener(this);

        adapter.setLoadListener(this);

    }

    public void getData() {
        HttpService service = new Retrofit.Builder()
                .baseUrl("http://www.baidu.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class);
        service.getSpeacialData(page++).enqueue(this);
    }

    @Override
    public void onResponse(Call<ClassifyEntity> call, Response<ClassifyEntity> response) {
        swipeRefreshLayout.setRefreshing(false);
        ClassifyEntity body = response.body();
        List<ClassifyEntity.TopicRecommendListBean> topicRecommendList = body.getTopicRecommendList();
        SpecialEntity specialEntity = new SpecialEntity();
        specialEntity.setList(topicRecommendList);
        list.add(0, specialEntity);
//        Toast.makeText(getContext(), ""+topicRecommendList.size(), Toast.LENGTH_SHORT).show();
        adapter.notifyDataSetChanged();
        AuthorEntity authorEntity = new AuthorEntity();
        authorEntity.setList(body.getAuthorList());
        list.add(1, authorEntity);
        adapter.notifyDataSetChanged();
        ArticleEntity articleEntity = new ArticleEntity();
        articleEntity.setList(body.getArticleList());
        list.add(2, articleEntity);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(Call<ClassifyEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(), "" + t.getMessage(), Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onRefresh() {
        page = 1;
        getData();
    }

    @Override
    public void loadMore() {
        getData();
    }

    private void initWindow() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }
}
